﻿using System;
using log4net;


//注意下面的语句一定要加上，指定log4net使用.config文件来读取配置信息（注入Log4Net配置信息，Log4Net的核心就在此）
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Common
{
    public class LogHelper
    {
        /// <summary>
        /// 默认的日志对象
        /// </summary>
        private static ILog Default
        {
            get { return LogManager.GetLogger("Default"); }
        }

        #region 日志配置信息

        /// <summary>
        /// Debug日志是否启用
        /// </summary>
        public static bool IsDebugEnabled
        {
            get
            {
                return Default.IsDebugEnabled;
            }
        }

        /// <summary>
        /// Error日志是否启用
        /// </summary>
        public static bool IsErrorEnabled
        {
            get
            {
                return Default.IsErrorEnabled;
            }
        }

        /// <summary>
        /// Fatal日志是否启用
        /// </summary>
        public static bool IsFatalEnabled
        {
            get
            {
                return Default.IsFatalEnabled;
            }
        }

        /// <summary>
        /// Info日志是否启用
        /// </summary>
        public static bool IsInfoEnabled
        {
            get
            {
                return Default.IsInfoEnabled;
            }
        }

        /// <summary>
        /// Warn日志是否启用
        /// </summary>
        public static bool IsWarnEnabled
        {
            get
            {
                return Default.IsWarnEnabled;
            }
        }

        #endregion

        #region 写日志

        #region 错误级别
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message">自定义消息（可选）</param>
        public static void Error(params string[] message)
        {
            if (IsErrorEnabled)
            {
                ILog log = LogManager.GetLogger("Default");
                if (message.Length > 0)
                {
                    log.Error(message);
                }
            }
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        public static void Error(string error)
        {
            if (IsErrorEnabled)
            {
                ILog log = LogManager.GetLogger("Default");
                log.Error(error);
            }
        }
        #endregion

        #region 严重错误级别
        /// <summary>
        /// 严重错误日志
        /// </summary>
        public static void WriteFatalLog(Type t, Exception ex, params string[] message)
        {
            if (IsFatalEnabled)
            {
                ILog log = LogManager.GetLogger(t);
                if (message.Length > 0)
                {
                    log.Fatal(message, ex);
                }
                else
                {
                    log.Fatal(ex);
                }
            }
            
        }

        /// <summary>
        /// 严重错误日志
        /// </summary>
        public static void WriteFatalLog(Type t, string fatal)
        {
            if (IsFatalEnabled)
            {
                ILog log = LogManager.GetLogger(t);
                log.Fatal(fatal);
            }
        }
        #endregion

        #region 信息级别
        /// <summary>
        /// 信息日志
        /// </summary>
        public static void WriteInfoLog(Type t, Exception ex, params string[] message)
        {
            if (IsInfoEnabled)
            {
                ILog log = LogManager.GetLogger(t);
                if (message.Length > 0)
                {
                    log.Info(message, ex);
                }
                else
                {
                    log.Info(ex);
                }
            }
        }

        /// <summary>
        /// 信息日志
        /// </summary>
        public static void WriteInfoLog(Type t, string info)
        {
            if (IsInfoEnabled)
            {
                ILog log = LogManager.GetLogger(t);
                log.Info(info);
            }
        }
        #endregion

        #region 调试级别
        /// <summary>
        ///  调试日志
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(params string[] message)
        {
            if (IsDebugEnabled)
            {
                ILog log = LogManager.GetLogger("Default");
                if (message.Length > 0)
                {
                    log.Debug(message);
                }
            }
            
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="debug"></param>
        public static void Debug(string debug)
        {
            if (IsDebugEnabled)
            {
                ILog log = LogManager.GetLogger("Default");
                log.Debug(debug);
            }
        }
        #endregion

        #region 警告级别
        /// <summary>
        /// 警告日志
        /// </summary>
        public static void WriteWarnLog(Type t, Exception ex, params string[] message)
        {
            if (IsWarnEnabled)
            {
                ILog log = LogManager.GetLogger(t);
                if (message.Length > 0)
                {
                    log.Warn(message, ex);
                }
                else
                {
                    log.Warn(ex);
                }
            }
        }

        /// <summary>
        /// 警告日志
        /// </summary>
        public static void WriteWarnLog(Type t, string warn)
        {
            if (IsWarnEnabled)
            {
                ILog log = LogManager.GetLogger(t);
                log.Warn(warn);
            }
        }
        #endregion

        #endregion



    }
}
